<script lang="ts" setup>
  import { ref, computed } from 'vue';
  import { apiUrl, formatDate, formatUrl } from "@/util";
  const card = ref(""),
    urls = computed(() => {
      const r = {
        bg1: "/static/my-bg.png",
        bg2: "/static/card-bg2.png",
        logo: "static/logo.png",
        account: "/static/account.png",
        pass: "/static/pass.png",
      };
      Object.keys(r).forEach(k => { r[k] = formatUrl(r[k], apiUrl); });
      return r;
    }),
    cp = ref("");
  async function tk() {
    if (card.value.length < 10 || cp.value.length < 6) {
      uni.showToast({
        title: '请输入正确的卡号和密码',
        icon: 'none'
      });
      return;
    }
    const res = await request({ id: card.value + cp.value }, "voucher/read");
    if (Array.isArray(res) && res.length) {
      const d = res[0],
        s = d.use ? `此卡于${formatDate(d.changeTime)}被使用` : "是否使用？";

      uni.showModal({
        title: '卡片信息',
        content: `卡片金额￥:${Number(d.money / 100).toFixed(2)}\n${s}`,
        confirmText: d.use ? '关闭' : '使用',
        cancelText: '取消',
        showCancel: !d.use,
        success: function (res) {
          if (res.confirm && !d.use) {
            request({ id: card.value + cp.value }, "voucher/use");
          }
        }
      });
    }
  }
</script>
<template>
  <view class="container">
    <view class="image">
      <image class="bg-1" mode="scaleToFill" :src="urls.bg1" />
      <image class="bg-2" :src="urls.bg2" />
      <view class="bg-5"></view>
    </view>
    <view class="content">
      <image :src="urls.logo" class="logo" />

      <view class="input-box">
        <image class="input-icon" :src="urls.account" />
        <text class="lb-text">卡号：</text>
        <input class="lb-input" placeholder="请输入卡号" v-model="card" />
      </view>
      <view class="input-box">
        <image class="input-icon" :src="urls.pass" />
        <text class="lb-text">密码：</text>
        <input class="lb-input" placeholder="请输入密码" v-model="cp" />
      </view>
      <view class="lb-btn" @click="tk">确认</view>

      <view class="describe">
        <view class="describe-title">提示说明</view>
        <view class="describe-content">
          <view>1、请确保所输入的卡号和密码是本人所拥有。</view>
          <view>2、提卡成功后，将卡片金额转入您的余额，请进入我的页面查看。</view>
          <view>3、提卡申请后，不可撤销，卡密将失效。</view>
        </view>
      </view>
    </view>
  </view>
</template>
<style lang="scss">
  page {
    background: #FFFFFF;
  }
  .container {
    height: 100%;
    height: 100%;
    .image {
      position: absolute;
      width: 100%;
      height: 100%;
      image {
        position: absolute;
        width: 100%;
        height: 100%;
      }
      .bg-1 {
        z-index: 1;
      }
      .bg-2 {
        z-index: 2;
      }
      .bg-5 {
        position: absolute;
        width: 100%;
        height: 100%;
        // 背景图渐变色 #C1261D, #FFFFFF
        background: linear-gradient(0deg, rgba(195, 54, 40, .7) 40%, rgba(255, 255, 255, .5));
        z-index: 3;
      }
    }
    .content {
      position: relative;
      z-index: 9;
      display: flex;
      flex-direction: column;
      align-items: center;
      padding-top: 4em;
      .logo {
        width: 10em;
        height: 10em;
        margin: 0 auto;
      }
      .input-box {
        background-color: #FFFFFF;
        border-radius: .2em;
        padding: .8em .8em;
        display: flex;
        justify-content: center;
        align-items: center;
        .input-icon {
          width: 1.1em;
          height: 1.1em;
        }
        .lb-text {
          font-size: .9em;
          margin: 0 .4em;
        }
        .lb-input {
          width: 450rpx;
        }
      }
      .input-box:nth-of-type(2) {
        margin-top: 1em;
      }
      .lb-btn {
        width: 80%;
        height: 2.5em;
        line-height: 2.5em;
        background-color: #C1261D;
        border-radius: 6em;
        color: #FFFFFF;
        text-align: center;
        margin-top: 2em
      }
      .lb-btn:nth-of-type(2) {
        background: rgba(000, 000, 000, .9);
        // background: rgba(195, 54, 40, .7);
      }
    }
    .describe {
      position: relative;
      z-index: 9;
      width: 650rpx;
      margin-top: 2em;
      background-color: rgba(255, 255, 255, .6);
      border-radius: .6em;
      padding: .8em;
      &-title {
        font-size: 1em;
        color: #ffeb3b;
      }
      &-content {
        margin-top: 1em;
        color: #FFFFFF;
        font-size: .85em;
        line-height: 1.5em;
      }
    }
  }
</style>